package com.qqclient.serivce;

import com.qqcommon.Message;
import com.qqcommon.MessageType;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.Scanner;

public class ClientConnectServerThread extends Thread{
    Scanner scanner = new Scanner(System.in);
    private Socket socket;

    public ClientConnectServerThread(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run(){
        //因为Thread需要在后台和服务器通信，因此我们需要while循环
        while (true){
            try {
                System.out.println("客户端线程，等待读取从服务端发送的消息");
                ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
                //无消息会阻塞
                Message message = (Message) ois.readObject();
                //如果读取到的服务端返回的用户在线列表信息
                if (message.getMesType().equals(MessageType.MESSAGE_RET_ONLINE_FRIEND)){
                    //取出信息并显示
                    String[] onlineUsers = message.getContent().split(" ");
                    System.out.println("\n======当前在线用户列表======");
                    for (int i = 0; i < onlineUsers.length; i++) {
                        System.out.println("用户："+onlineUsers[i]);
                    }
                }
                //文件
                else if (message.getMesType().equals(MessageType.MESSAGE_FILE_MES)){
                    System.out.println("\n"+message.getSendTime()+" "+message.getSender()+"用户："+message.getContent()+" "+message.getDest()+"下传输文件");
                    FileOutputStream fileOutputStream = new FileOutputStream(message.getDest());
                    fileOutputStream.write(message.getFileBytes());
                    if (fileOutputStream != null){
                        fileOutputStream.close();
                    }
                    System.out.println("保存文件成功");

                }
                //群发
                else if (message.getMesType().equals(MessageType.MESSAGE_TO_ALL_MES)){
                    System.out.println("\n"+message.getSendTime()+" "+message.getSender()+"用户："+message.getContent());
                }
                //私聊
                else if (message.getMesType().equals(MessageType.MESSAGE_COMM_MES)){
                    //显示消息
                    System.out.println("\n"+message.getSendTime()+" "+message.getSender()+"用户："+message.getContent());
//                    Message message1 = new Message();
//                    message1.setSender(message.getGetter());
//                    System.out.print("请输入聊天对象：");
//                    String g = scanner.next();
//                    message1.setGetter(g);
//                    System.out.print(message.getSender()+"：");
//                    String s = scanner.next();
//                    message1.setContent(s);
//                    ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
//                    oos.writeObject(message1);

                }
                else {
                    System.out.println("其他类型暂不处理");
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public Socket getSocket() {
        return socket;
    }
}
